import { createRouter, createWebHistory } from 'vue-router'
interface Route {
  path: string
  name?: string
  component?: any
  title?: string
  icon?: string
}

export const childroute = [
  {
    path: '/home/Availability',
    name: 'Availability',
    icon: "Coin",

    meta: { requiresAuth: true },
 
    component: () => import('../views/Availability/Availability.vue'),
    title: '房源管理'
  },
  {
    path: '/home/live',
    name: 'live',
    icon: "VideoCamera",

    meta: { requiresAuth: true },

    component: () => import('../views/live/Live.vue'),
    title: '直播管理'
  },
  {
    path: '/home/Buy',
    name: 'Buy',
    icon: "Football",

    meta: { requiresAuth: true },

    component: () => import('../views/Buy/Buy.vue'),
    title: '团购管理'
  },
  {
    path: '/home/Audit',
    name: 'Audit',
    icon: "Notebook",
    meta: { requiresAuth: true },
    component: () => import('../views/Audit/Audit.vue'),
    title: '审核管理'
  },
  {
    path: '/home/Orders',
    name: 'Orders',
    icon: "ShoppingTrolley",
    meta: { requiresAuth: true },
    component: () => import('../views/Orders/Orders.vue'),
    title: '订单管理'
  },
  {
    path: '/home/Information',
    name: 'Information',
    icon: "List",
    meta: { requiresAuth: true },
    component: () => import('../views/Information/Information.vue'),
    title: '资讯管理'
  },
  {
    path: '/home/Broker',
    name: 'Broker',
    icon: "House",
    meta: { requiresAuth: true },
    component: () => import('../views/Broker/Broker.vue'),
    title: '经纪人管理'
  },
  {
    path: '/home/Employee',
    name: 'Employee',
    icon: "Shop",
    meta: { requiresAuth: true },
    component: () => import('../views/Employee/Employee.vue'),
    title: '员工管理'
  },
  {
    path: '/home/User',
    name: 'User',
    icon: "User",
    meta: { requiresAuth: true },
    component: () => import('../views/User/User.vue'),
    title: '用户管理'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login/Login.vue')
    },
    {
      path: '/home',
      name: 'home',
      meta: { requiresAuth: true },
      component: () => import('../views/home/Index.vue'),
      children: childroute
    }
  ]
})
// 定义全局前置守卫（beforeEach）  
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证  
  if (to.matched.some(record => record.meta.requiresAuth)) {
    console.log("需要验证");
    
    // 在这里可以进行认证逻辑，例如检查用户是否已登录  
    // 如果用户已登录，可以调用 next() 进入目标页面，否则可以重定向到登录页面。  
    // 这里只是一个简单的示例，你可以根据你的实际需求进行修改。  
    const isAuthenticated = /* 进行认证逻辑 */localStorage.getItem('token');
    if (isAuthenticated) {
      next(); // 认证成功，进入目标页面  
    } else {
      alert('登录超时请重新登录')
      next({ name: 'Login' }); // 认证失败，重定向到登录页面  
    }
  } else {
    // 路由不需要认证，直接进入目标页面  
    next();
  }
});
export default router
